//
//  ChooseAddressCtrl.m
//  laowu
//
//  Created by Northwan on 15/5/8.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "ChooseAddressCtrl.h"
#import "LocationDao.h"
#import "PublishDateCell.h"
#import "AddressDbBean.h"

@interface ChooseAddressCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property(weak,nonatomic)IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *currentCityLabel;
@property(strong,nonatomic) LocationDao *locationDao;

@property (nonatomic, strong) NSMutableArray *provincesArray;
@property (nonatomic, strong) NSMutableArray *cityArray;

@end

@implementation ChooseAddressCtrl


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"选择地区"];
    NSString *city = [UserDefaultsHelper stringForKey:kLocationAddressCity];
    if(ISBLANK(city)){
        [_currentCityLabel setText:@"全国"];
    }else{
        [_currentCityLabel setText:city];
    }
    
    _locationDao = [[LocationDao alloc]init];
    
    _provincesArray = [NSMutableArray arrayWithArray:[_locationDao getProvinces]];
    _cityArray = [NSMutableArray array];
    for (AddressDbBean *address in _provincesArray) {
        [_cityArray addObject:[_locationDao getCitys:address.provinceId]];
    }

    [self initTableView];
}

-(IBAction)clickCurrentCity{
   [[NSNotificationCenter defaultCenter]postNotificationName:nortif_address_choose_finish object:_currentCityLabel.text];
   [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)initTableView{
    
    UIButton *btn = [[UIButton alloc] init];
    [btn setBackgroundColor:RGBColor(240, 240, 240)];
    btn.height = 40;
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:@"全国" forState:UIControlStateNormal];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn addTarget:self action:@selector(tableViewheaderClick:) forControlEvents:UIControlEventTouchUpInside];
    [_tableView setTableHeaderView:btn];
    
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
}

-(void)tableViewheaderClick:(UIButton *)btn{
    [[NSNotificationCenter defaultCenter]postNotificationName:nortif_address_choose_finish object:@"全国"];
    [self.navigationController popViewControllerAnimated:YES];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _provincesArray.count;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([_provincesArray[section] isOpen]) {
        return [_cityArray[section] count];
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PublishDateCell *cell = [PublishDateCell cellWithTableView:tableView indexPath:indexPath];
    AddressDbBean *bean = _cityArray[indexPath.section][indexPath.row];
    [cell initData:[bean cityName]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return PublishDateCellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [[NSNotificationCenter defaultCenter]postNotificationName:nortif_address_choose_finish object:[_cityArray[indexPath.section][indexPath.row] cityName]];
    [self.navigationController popViewControllerAnimated:YES];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIButton *btn = [[UIButton alloc] init];
    [btn setBackgroundColor:RGBColor(240, 240, 240)];
    btn.height = 40;
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:[_provincesArray[section] provinceName] forState:UIControlStateNormal];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [btn setTag:section];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn addTarget:self action:@selector(headerViewClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}


-(void)headerViewClick:(UIButton *)btn{
    NSInteger section = btn.tag;
    AddressDbBean *bean = _provincesArray[section];
    bean.isOpen = !bean.isOpen;
    [self.tableView reloadData];
}

@end
